package dbtest;

import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Henrik
 */
public class Game {
    public static final String[] PIECE_NAMES = {
        "b_king",
        "b_queen",
        "b_bishop1",
        "b_bishop2",
        "b_knight1",
        "b_knight2",
        "b_rook1",
        "b_rook2",
        "b_pawn1",
        "b_pawn2",
        "b_pawn3",
        "b_pawn4",
        "b_pawn5",
        "b_pawn6",
        "b_pawn7",
        "b_pawn8",
        "w_king",
        "w_queen",
        "w_bishop1",
        "w_bishop2",
        "w_knight1",
        "w_knight2",
        "w_rook1",
        "w_rook2",
        "w_pawn1",
        "w_pawn2",
        "w_pawn3",
        "w_pawn4",
        "w_pawn5",
        "w_pawn6",
        "w_pawn7",
        "w_pawn8"
    };
    public static final int BLACK = 0;
    public static final int WHITE = 1;
    private ArrayList<Piece> pieces;
    private DbConnector db;
    public int turn;
    private int gameId;
    private int activePlayer;
    ChessFrame frame;
    ChessPanel chessPanel;
    SidePanel sidepanel;
    HighScoreFrame Hsf;
    private Piece selectedPiece;
    
    public Game(int gameId) {
        this.gameId = gameId;
        
        try {
            db = new DbConnector();
        } catch (InstantiationException | IllegalAccessException | SQLException | ClassNotFoundException e) {
            System.out.println(e);
        }
        if (db.getTurnNumber(gameId) % 2 == 0) {
            activePlayer = BLACK;
        } else {
            activePlayer = WHITE;
        }
        frame = new ChessFrame();
        chessPanel = new ChessPanel(this);    
        try{
        sidepanel = new SidePanel(this.gameId,db.getPlayer1(gameId),db.getPlayer2(gameId),db.getTurnNumber(gameId), db, this);
        } catch(SQLException ex){
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
        }
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(chessPanel, BorderLayout.CENTER);
        frame.getContentPane().add(sidepanel, BorderLayout.EAST);
        frame.setVisible(true);
    }
    
    public int getActivePlayer() {
        return activePlayer;
    }
    
    public void submitMove(Piece p) {
        System.out.println(p.getPos());
        try {
            db.makeMove(gameId, p.getPieceName(), p.getPos());
        } catch (InstantiationException | IllegalAccessException | SQLException | ClassNotFoundException ex) {
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("turn: " + db.getTurnNumber(gameId));
        if (db.getTurnNumber(gameId) % 2 == 0) {
            activePlayer = BLACK;
        } else {
            activePlayer = WHITE;
        }
    }
    
    public void loadGamestate(int gameid) {
        pieces.clear();
        ArrayList<String> board;
        
        try {
            board = db.getBoard(gameid);
            
            for (String s : board) {
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Piece getSelectedPiece() {
        return selectedPiece;
    }

    public void setSelectedPiece(Piece selectedPiece) {
        this.selectedPiece = selectedPiece;
    }
    
    public void ShowHighScores() throws SQLException{
    
        Hsf = new HighScoreFrame(db);
    
    }
}
